using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using WeifenLuo.WinFormsUI.Docking;

namespace ApplicationManager
{
    public partial class applicationmenuForm : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        private string StartupPath = Application.StartupPath;
        private frmMain myParentForm = null;

        public applicationmenuForm
            (
                    frmMain myParentForm
            )
        {
            this.myParentForm = myParentForm;
            GlobalInfo.OpenNewForm += new GlobalInfo.GlobalInfoFormEventHandler(globalInfo_OpenForm);
            GlobalInfo.CreateNewForm += new GlobalInfo.GlobalInfoFormEventHandler(globalInfo_CreateNewForm);
            GlobalInfo.OpenNewDataManager += new GlobalInfo.GlobalInfoFormEventHandler(globalInfo_OpenDataManager);
            InitializeComponent();
        }

        private Form globalInfo_CreateNewForm(object sender, GlobalInfoFormArgs pe)
        {
            return (DocumentForm)DllManager.CreateIstance(pe.Namespace, null);
        }

        private Form globalInfo_OpenForm(object sender, GlobalInfoFormArgs pe)
        {
            DocumentForm frm = (DocumentForm)DllManager.CreateIstance(pe.Namespace, null);
            frm.OpenForm += new DocumentForm.OpenFormEventHandler(frm_OpenForm);
            AttachScrounger(frm, pe.Namespace);
            return ApriForm(frm, pe.Modal);
        }

        private Form globalInfo_OpenDataManager(object sender, GlobalInfoFormArgs pe)
        {
            DocumentForm frm = (DocumentForm)DllManager.CreateIstance(pe.Namespace, null);
            frm.OpenForm += new DocumentForm.OpenFormEventHandler(frm_OpenForm);
            AttachScrounger(frm, pe.Namespace);
            frm.ShowDialog(pe.ShowDialog);
            frm.SilentMode = !pe.ShowDialog;
            return frm;
        }

        private void menuForm_Load(object sender, System.EventArgs e)
        {
            CreaMenu();
            myParentForm.AddFormToMenu(this);
        }

        private void CreaMenu()
        {
            this.treeMenu = new TreeMenu();
            this.treeMenu.Dock = DockStyle.Fill;
            this.treeMenu.NodeClick += new EventHandler(treeMenu_NodeClick);
            this.Controls.Add(this.treeMenu);

            foreach (ApplicationMenuModule module in ModuleManager.ModuleList)
                if (DisplayMenu(module.MenuFolders))
                    treeMenu.AddButton(module);
        }

        private bool DisplayMenu(List<ApplicationMenuFolder> amf)
        {
            foreach (ApplicationMenuFolder folder in amf)
                if (!DisplayFolder(folder))
                    return false;

            return true;
        }

        private bool DisplayFolder(ApplicationMenuFolder amf)
        {
            foreach (ApplicationMenuItem menu in amf.MenuItems)
                if (menu.UserGrant <= GlobalInfo.UserInfo.userType)
                    return true;

            return false;
        }

        #region Open & Close Form

        private Form ApriForm(Form form, bool modal)
        {
            try
            {
                myParentForm.ApriForm(form, modal);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }

            return form;
        }

        #endregion

        #region Menu Event

        private void treeMenu_NodeClick(object sender, EventArgs e)
        {
            TreeMenuNode node = (TreeMenuNode)sender;
            ApplicationMenuItem AMI = node.Item;

            try
            {
                switch (AMI.DocumentType)
                {
                    case FormType.Document:
                        DocumentForm frm = null;
                        if (frm == null)
                        {
                            frm = (DocumentForm)DllManager.CreateIstance(AMI.Namespace, null);
                            frm.OpenForm += new DocumentForm.OpenFormEventHandler(frm_OpenForm);
                            AttachScrounger(frm, AMI.Namespace);
                        }
                        ApriForm(frm, false);
                        break;

                    case FormType.Batch:
                        BatchForm bfm = null;
                        bfm = (BatchForm)DllManager.CreateIstance(AMI.Namespace, null);
                        bfm.OpenForm += new BatchForm.OpenFormEventHandler(frm_OpenForm);
                        ApriForm(bfm, false);
                        break;

                    case FormType.Report:
                        ReportForm rfm = null;
                        rfm = (ReportForm)DllManager.CreateIstance(AMI.Namespace, null);
                        rfm.OpenForm += new ReportForm.OpenFormEventHandler(frm_OpenForm);
                        ApriForm(rfm, false);
                        break;

                    case FormType.Generic:
                        DockContent gfm = null;
                        gfm = (DockContent)DllManager.CreateIstance(AMI.Namespace, null);
                        //gfm.OpenForm += new ReportForm.OpenFormEventHandler(frm_OpenForm);
                        ApriForm(gfm, false);
                        break;
                }
            }
            catch (Exception exc)
            {
                string msg = exc.Message;
                MessageBox.Show(msg);
            }
        }

        private void AttachScrounger(iScrounger frm, NameSpace nameSpace)
        {
            foreach (ApplicationMenuModule amm in ModuleManager.ModuleList)
            {
                string module = amm.Namespace.Module;
                amm.Namespace.Application = "ModuleData.RegisterModule";
                RegisterModule registerModule = (RegisterModule)DllManager.CreateIstance(amm.Namespace, null);
                if (registerModule != null)
                {
                    registerModule.AddScrounger(frm, nameSpace);
                }
            }
        }

        public void PanelEvent(object sender, EventArgs e)
        {
            PanelIcon pI = (PanelIcon)sender;
            NameSpace ns = new NameSpace(pI.Application);
            ApplicationMenuItem AMI = null;// = moduleManager.AMC.KeyMenuItem[pI.Application];

            try
            {
                switch (AMI.DocumentType)
                {
                    case FormType.Document:
                        DocumentForm frm = null;
                        if (frm == null)
                        {
                            frm = (DocumentForm)DllManager.CreateIstance(ns, null);
                            frm.OpenForm += new DocumentForm.OpenFormEventHandler(frm_OpenForm);
                        }
                        ApriForm(frm, false);
                        break;
                }
            }
            catch (Exception exc)
            {
                string msg = exc.Message;
                MessageBox.Show(msg);
            }
        }

        private void frm_OpenForm(Form frm, bool modal)
        {
            ApriForm(frm, modal);
        }

        #endregion

        private void applicationmenuForm_Activated(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.Dock = DockStyle.Left;
        }
    }
}